/**
 * @(#)MiguProvinceController.java 2017年7月7日
 * Copyright(C) 2017 ZHEXIN IT CO.,LTD. All rights reserved.
 */
package org.jsbd.boss.action;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Set;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import jodd.util.StringUtil;

import org.jsbd.boss.common.ResponseUtils;
import org.jsbd.boss.dao.IMiguProvinceConfigDao;
import org.jsbd.boss.domian.ProvinceInfo;
import org.jsbd.boss.domian.channel.ChannelApkConfig;
import org.jsbd.boss.domian.channel.MiguProvince;
import org.jsbd.boss.service.IIpService;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

/**
 * TODO 请加入本类的说明
 * 
 * @author <a href="mailto:xupengcheng@zhexinit.com" >徐鹏程</a>
 * @version 1.0.0
 */
@Controller
@RequestMapping("miguProvince")
public class MiguProvinceController {

	public static final Logger LOGGER = LoggerFactory.getLogger(MiguUserController.class);
	@Resource
	private IMiguProvinceConfigDao miguProvinceConfigDao;

	@Resource
	private IIpService ipService;

	@RequestMapping(value = "/save", method = RequestMethod.POST)
	public void save(HttpServletResponse response, String openProvince) {
		try {
			if (StringUtil.isNotBlank(openProvince)) {
				String[] provinces = openProvince.split(",");
				List<MiguProvince> list = new ArrayList<MiguProvince>();
				for (String province : provinces) {
					MiguProvince miguProvince = new MiguProvince();
					miguProvince.setProvinceId(Integer.parseInt(province));
					list.add(miguProvince);
				}
				miguProvinceConfigDao.batchInsertMiguProvinceConfig(list);
				ResponseUtils.responseSuccess(response);
			}
		} catch (Exception e) {
			LOGGER.error("保存咪咕省份信息失败", e);
			ResponseUtils.responseFailure(response);
		}
	}

	@RequestMapping(value = "/{provinceId}/delete")
	public void addLog(HttpServletResponse response, @PathVariable Integer provinceId) {
		try {
			miguProvinceConfigDao.deleteMiguProvinceConfig(provinceId);
			ResponseUtils.responseSuccess(response);
		} catch (Exception e) {
			LOGGER.error("删除咪咕省份信息失败", e);
			ResponseUtils.responseFailure(response);
		}
	}

	@RequestMapping(value = "list", method = RequestMethod.GET)
	public String list() {
		return "/miguprovince/configList";
	}

	@RequestMapping(value = "addProvince", method = RequestMethod.GET)
	public String addProvince(Model model) {
		List<ProvinceInfo> provinceList = ipService.getProvinceInfoList();
		List<MiguProvince> markProvinceList = miguProvinceConfigDao.selectMiguProvinceConfigList(null);
		Set<Integer> provinceSet = new HashSet<>();
		if (markProvinceList != null && markProvinceList.size() > 0) {
			for (MiguProvince province : markProvinceList) {
				Integer procinceId = province.getProvinceId();
				provinceSet.add(procinceId);
			}
		}
		if (provinceList != null && provinceList.size() > 0) {
			Iterator<ProvinceInfo> itetator = provinceList.iterator();
			while (itetator.hasNext()) {
				ProvinceInfo province = itetator.next();
				Integer provinceId = province.getId();
				if (provinceId == -1) {
					itetator.remove();
				} else {
					if (provinceSet.contains(provinceId)) {
						province.setChecked(true);
					} else {
						province.setChecked(false);
					}
				}
			}
		}
		model.addAttribute("provinceList", provinceList);
		return "/miguprovince/addProvince";
	}

	@RequestMapping(value = "configList", params = "json")
	@ResponseBody
	public Map<String, Object> configPageList(ChannelApkConfig channelApkInfo) {
		Map<String, Object> map = new HashMap<String, Object>();
		List<MiguProvince> provinceList = miguProvinceConfigDao.selectMiguProvinceConfigList(null);
		map.put("rows", provinceList);
		map.put("total", provinceList.size());
		return map;
	}
}
